package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GridOrderAdapter;
import com.zhongxi.guoyuan.entity.center.RewardDoorResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.JsonUtils;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/1/20.
 */

public class RewardDoorAdapter extends BaseAdapter {

    private Context mContext;
    private List<RewardDoorResult.RewardDoorData> list;
    private String thumbnail;
    private String flag;

    public RewardDoorAdapter(Context context, List<RewardDoorResult.RewardDoorData> doorData, String thumbnail, String flag) {
        this.mContext = context;
        this.list = doorData;
        this.thumbnail = thumbnail;
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_reward_door, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                holder.tvReward = (TextView) convertView.findViewById(R.id.tvReward);
                holder.ivDel = (ImageView) convertView.findViewById(R.id.ivDel);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            try{
                if(null!=list.get(position)&&!"".equals(list.get(position))){
                    final RewardDoorResult.RewardDoorData data = list.get(position);
                    if(null!=data.getId()&&!"".equals(data.getId())){
                        holder.tvOrderId.setText("订单号码："+data.getId());
                    }else{
                        holder.tvOrderId.setText("订单号码：**");
                    }
                    if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                        holder.tvTime.setText("完成时间："+data.getCtime());
                    }else{
                        holder.tvTime.setText("完成时间：**");
                    }
                    if(null!=data.getGoodsnum()&&!"".equals(data.getGoodsnum())){
                        holder.tvNum.setText("商品数量："+data.getGoodsnum());
                    }else{
                        holder.tvNum.setText("商品数量：**");
                    }
                    if(null!=data.getPrice()&&!"".equals(data.getPrice())){
                        holder.tvCash.setText(data.getPrice()+"元");
                        holder.tvReward.setText("[返"+new BigDecimal(data.getPrice()).intValue()+"银币]");
                    }else{
                        holder.tvCash.setText("**元");
                        holder.tvReward.setText("[返**银币]");
                    }

                    if(data.isSelect()){
                        holder.ivDel.setImageResource(R.mipmap.iv_select);
                    }else{
                        holder.ivDel.setImageResource(R.mipmap.iv_noselect);
                    }
                    //删除选中
                    holder.ivDel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(data.isSelect()){
                                data.setSelect(false);
                            }else{
                                data.setSelect(true);
                            }
                            notifyDataSetChanged();
                        }
                    });
                    if(null!=thumbnail&&!"".equals(thumbnail)){
                        JSONObject obj = new JSONObject(thumbnail);
                        JSONArray ary = obj.getJSONArray(data.getId());
                        List<String> imgList = JsonUtils.getData(ary);
                        if(imgList.size()>1){
                            holder.ivImg.setVisibility(View.GONE);
                            holder.gvList.setVisibility(View.VISIBLE);
                            holder.gvList.setAdapter(new GridOrderAdapter(mContext, imgList));
                        }else{
                            holder.ivImg.setVisibility(View.VISIBLE);
                            holder.gvList.setVisibility(View.GONE);
                            Glide.with(mContext).load(Url.IMG_URL + imgList.get(0)).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public String getOrderids() {
        String str = "";
        for(int i=0;i<list.size();i++){
            if(list.get(i).isSelect())
                str += list.get(i).getId()+",";
        }
        return str.substring(0, str.length()-1);
    }

    class ViewHolder{
        private ImageView ivImg;
        private NoScrollGridView gvList;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvNum;
        private TextView tvCash;
        private TextView tvReward;
        private ImageView ivDel;
    }
}
